var fs = require("fs");
//var piexif = require("./piexif.js");
var http = require('http');
//require("//api.map.baidu.com/api?v=3.0&ak=BA7a941611feddd5977215b8b08afbb8")

function Get(src, cb)
{
	//console.log(src);
	http.get(src, function(res){
		let err;
		const { statusCode } = res;
		let rawData = "";

		if (!statusCode === 200) 
			err = new Error("服务器响应失败");
		//if (!/application\/json/.test(res.headers["content-type"]))
		//	 err = new Error("数据格式错误，需要json格式");
		if (err) {
			console.log(err);
			//释放内存
			res.resume();
			return;
		}

		// chunk是16进制BUFFER数据，需要转成字符打印
		res.on("data", (chunk) => {
			rawData += chunk;
		});

		//监听请求结束
		res.on("end", () => {
			try {
				//console.log(rawData);
				cb(rawData);
			} catch (e) {
				console.log(e);
			}
		});
	});
}

function DoOne()
{
	if (!data[i].endsWith("road") && !data[i].endsWith("ROAD"))
	{
		i++;
		if (i < data.length)
			DoOne();
		return;
	}
	console.log(data[i]);
	
	filecon = fs.readFileSync("image\\" + data[i], 'utf-8');
	//console.log(filecon);
	
	rrr = JSON.parse(filecon);
	console.log(rrr);
	CheckOne();
}

function CheckOne()
{
	for (var sid = 0; sid < rrr.subpaths.length; sid++)
	{
		var sp = rrr.subpaths[sid];
		//console.log(sp.type);
		
		if (sp.category == "bus" || sp.category == "metro")
		{
			console.log(sp.text, sp.from, sp.to);
			
			if (sp.number == null || sp.number == "")
			{
				console.log("sp.text == null");
				return;
			}
						
			var tag = "公交车站";
			if (sp.number.indexOf("地铁") >= 0)
				tag = "地铁站";
			
			if (sp.from == null || sp.from == "")
			{
				var p = sp.points[0];
				console.log(p);
				_sid = sid;
				_type = "from";
				Get("http://api.map.baidu.com/place/v2/search?query=" + tag + "&location=" + p[1] + "," + p[0] + "&radius=500&output=json&ak=BA7a941611feddd5977215b8b08afbb8&tag=" + tag + "&coord_type=2&page_size=20", callback);
				return;
			}
			if (sp.to == null || sp.to == "")
			{
				var p = sp.points[sp.points.length - 1];
				console.log(p);
				_sid = sid;
				_type = "to";
				Get("http://api.map.baidu.com/place/v2/search?query=" + tag + "&location=" + p[1] + "," + p[0] + "&radius=500&output=json&ak=BA7a941611feddd5977215b8b08afbb8&tag=" + tag + "&coord_type=2&page_size=20", callback);
				return;
			}
		}
	}
	
	DoOne2();
	return;
}

function callback(data){
	data = JSON.parse(data);
	//console.log(data);
	if (data.results != null && data.results.length > 0)
	{
		var r = null;
		for (var rid = 0; rid < data.results.length; rid++)
			if (data.results[rid].detail == 0)
			{
				r = data.results[rid].name;
				break;
			}
		if (r == null)
		{		
			console.log("r == null");
			return;
		}
		console.log(r);
		var p = rrr.subpaths[_sid];
		if (_type == "from")
			p.from = r;
		if (_type == "to")
			p.to = r;
		
		CheckOne();
	}
	else
	{
		console.log("data.results == null");
		return;
	}
}

function DoOne2()
{	
	fs.writeFileSync("out\\" + data[i], JSON.stringify(rrr), 'utf-8');
	
	i++;
	if (i < data.length)
		DoOne();
}

fs.readdir("image", function (err, dat){
	if (err)
	{
		console.log(err);
	}
	else
	{
		data = dat;
		i = 0;
		DoOne();
	}
});